package step

import (
	_interface "adam2/internal/quant/interface"
)

// 过滤器步骤
type FilterStep struct {
	// 统计日过滤器数组
	statisticsFilterArray *[]_interface.Filter
	// 买入日过滤器数组
	buyDatefilterArray *[]_interface.Filter
	buyDate            *_interface.BuyDate
	statisticsDate     *_interface.StatisticsDate
}

// 初始化
func (f *FilterStep) Init(buyDate *_interface.BuyDate, statisticsDate *_interface.StatisticsDate) {
	f.buyDate = buyDate
	f.statisticsDate = statisticsDate
}

// 添加统计日过滤器
func (f *FilterStep) AddStatisticsDateFilter(filter *_interface.Filter) {
	if f.statisticsFilterArray == nil {
		f.statisticsFilterArray = new([]_interface.Filter)
	}
	*f.statisticsFilterArray = append(*f.statisticsFilterArray, *filter)
}

// 添加买入日过滤器
func (f *FilterStep) AddBuyDateFilter(filter *_interface.Filter) {
	if f.buyDatefilterArray == nil {
		f.buyDatefilterArray = new([]_interface.Filter)
	}
	*f.buyDatefilterArray = append(*f.buyDatefilterArray, *filter)
}

// 按顺序执行过滤器
func (f *FilterStep) Exec(transactionDate string) {
	// 统计日
	if (*f.statisticsDate).IsStatisticsDate(transactionDate) {
		if len(*f.statisticsFilterArray) > 0 {
			for _, filter := range *f.statisticsFilterArray {
				filter.DoFilter(transactionDate)
			}
		}
	}
	// 买入日
	if (*f.buyDate).IsBuyDate(transactionDate) {
		if len(*f.buyDatefilterArray) > 0 {
			for _, filter := range *f.buyDatefilterArray {
				filter.DoFilter(transactionDate)
			}
		}
	}
}
